package com.fromfuture.xuhuidoc.android.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fromfuture.xuhuidoc.R;
import com.fromfuture.xuhuidoc.android.activity.BlActivity;
import com.fromfuture.xuhuidoc.android.activity.ImageActivity;
import com.fromfuture.xuhuidoc.android.activity.PatDetailsActivity;
import com.fromfuture.xuhuidoc.android.base.BaseWebActivity;
import com.fromfuture.xuhuidoc.android.bean.MyConsultRoom02RecycleViewInfo;
import com.fromfuture.xuhuidoc.cordova.callback.OnImageSuccessListener;
import com.fromfuture.xuhuidoc.cordova.untils.CordovaAppManager;
import com.fromfuture.xuhuidoc.cordova.untils.ImageTask;

import java.util.List;

import app.auto.runner.base.utility.ToastUtil;

/**
 * Author：何送军
 * creation time：2017/8/29
 **/
public class MyConsultRoomAdapter02 extends RecyclerView.Adapter<MyConsultRoomAdapter02.MyConsultRoomViewHolders02> {

    Context context;
    List<MyConsultRoom02RecycleViewInfo> list = null;
    List<String> list_isdocpat;
    List<Bitmap> bitmapList;

    public MyConsultRoomAdapter02(Context context, List<MyConsultRoom02RecycleViewInfo> list, List<String> list_isdocpat) {
        this.context = context;
        this.list = list;
        this.list_isdocpat = list_isdocpat;
    }

    public void MyConsultRoomAddData(int pos, MyConsultRoom02RecycleViewInfo e) {
        list.set(pos, e);
        notifyItemChanged(pos);
    }

    @Override
    public MyConsultRoomViewHolders02 onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.my_consult_room_02_list_item, parent, false);
        return new MyConsultRoomViewHolders02(view);
    }

    @Override
    public void onBindViewHolder(final MyConsultRoomViewHolders02 holder, final int position) {
        if (list_isdocpat.get(position).equals("doc")) {
            holder.linearLayout_left.setVisibility(View.INVISIBLE);
            holder.linearLayout_right.setVisibility(View.VISIBLE);
        } else if (list_isdocpat.get(position).equals("pat")) {
            holder.linearLayout_left.setVisibility(View.VISIBLE);
            holder.linearLayout_right.setVisibility(View.INVISIBLE);
        }
        holder.textView_left_name.setText(list.get(position).getTextView_left_name());
        holder.textView_right_name.setText(list.get(position).getTextView_right_name());
        holder.textView_left_text.setText(list.get(position).getTextView_left_text());
        holder.textView_right_text.setText(list.get(position).getTextView_right_text());
        if (list.get(position).getPatblguid() != null) {
            holder.textView_right_bl.setText("点击查看病例");
            holder.textView_right_bl.setVisibility(View.VISIBLE);
            holder.textView_right_bl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(CordovaAppManager.myConsultRoomActivity, BlActivity.class);
                    intent.putExtra("lookguid", list.get(position).getPatblguid());
                    CordovaAppManager.myConsultRoomActivity.startActivity(intent);
                }
            });
        } else {
            holder.textView_right_bl.setVisibility(View.GONE);
        }

        if (list.get(position).getBitmap_left_url() != "") {
            holder.textView_left_text.setVisibility(View.GONE);
            holder.imageView_left_img.setVisibility(View.VISIBLE);

            ImageTask imageTask = new ImageTask(new OnImageSuccessListener() {
                @Override
                public void onImageSuccess(Bitmap bitmap) {
                    holder.imageView_left_img.setImageBitmap(bitmap);
                    list.get(position).setBitmap_left(bitmap);
                }
            });
            imageTask.execute(list.get(position).getBitmap_left_url() + "&usrid=15581554261&userpwd=123456");
            holder.imageView_left_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(CordovaAppManager.myConsultRoomActivity, ImageActivity.class);
//                    intent.putExtra("bitmap",list.get(position).getBitmap_left());
                    intent.putExtra("imgurl", list.get(position).getBitmap_left_url() + "&usrid=15581554261&userpwd=123456");
                    CordovaAppManager.myConsultRoomActivity.startActivity(intent);
                }
            });

        } else {
            holder.textView_left_text.setVisibility(View.VISIBLE);
            holder.imageView_left_img.setVisibility(View.GONE);
        }

        if (list.get(position).getBitmap_right() != null) {
            holder.imageView_right_img.setVisibility(View.VISIBLE);
            holder.imageView_right_img.setImageBitmap(list.get(position).getBitmap_right());
            holder.imageView_right_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(CordovaAppManager.myConsultRoomActivity, ImageActivity.class);
                    intent.putExtra("bitmap", list.get(position).getBitmap_big());
                    CordovaAppManager.myConsultRoomActivity.startActivity(intent);
                }
            });
        } else {
            holder.imageView_right_img.setVisibility(View.GONE);
        }


        holder.imageView_left_tx_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                view.getContext().startActivity(new Intent(view.getContext(), BaseWebActivity.class).putExtra("name", "patient_details.html?usrGuid=" + list.get(position).getPatguid()));
                ToastUtil.get((Activity) view.getContext()).toast(list.get(position).getPatguid());

//                Intent intent=new Intent(CordovaAppManager.myConsultRoomActivity, PatDetailsActivity.class);
//                Log.d("患者详情：",list.get(position).getPatguid());
//                intent.putExtra("PatGuid",list.get(position).getPatguid());
//                CordovaAppManager.myConsultRoomActivity.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyConsultRoomViewHolders02 extends RecyclerView.ViewHolder {
        LinearLayout linearLayout_left, linearLayout_right;
        TextView textView_left_name, textView_left_text, textView_right_name, textView_right_text, textView_right_bl;
        ImageView imageView_left_tx_img, imageView_right_tx_img, imageView_left_img, imageView_right_img;

        public MyConsultRoomViewHolders02(View itemView) {
            super(itemView);

            linearLayout_left = (LinearLayout) itemView.findViewById(R.id.my_consult_room_02_chat_list_left);
            linearLayout_right = (LinearLayout) itemView.findViewById(R.id.my_consult_room_02_chat_list_right);
            textView_left_name = (TextView) itemView.findViewById(R.id.my_consult_room_02_chat_list_left_name);
            textView_right_name = (TextView) itemView.findViewById(R.id.my_consult_room_02_chat_list_right_name);
            textView_left_text = (TextView) itemView.findViewById(R.id.my_consult_room_02_chat_list_left_text);
            textView_right_text = (TextView) itemView.findViewById(R.id.my_consult_room_02_chat_list_right_text);
            textView_right_bl = (TextView) itemView.findViewById(R.id.my_consult_room_02_chat_list_right_lookbl);
            imageView_left_tx_img = (ImageView) itemView.findViewById(R.id.my_consult_room_02_chat_list_left_tx_img);
            imageView_right_tx_img = (ImageView) itemView.findViewById(R.id.my_consult_room_02_chat_list_right_tx_img);
            imageView_right_img = (ImageView) itemView.findViewById(R.id.my_consult_room_02_chat_list_right_img);
            imageView_left_img = (ImageView) itemView.findViewById(R.id.my_consult_room_02_chat_list_left_img);

        }


    }


    public interface IsDocPatSendMsg {
        public void who_send(int pos, String who);
    }

}
